<template>
  <div>
    <el-form :model="ruleForm2"
             status-icon
             :rules="rules2"
             ref="ruleForm2"
             label-width="100px"
             class="demo-ruleForm">
      <el-form-item label="用户"
                    prop="username">
        <el-input v-model="ruleForm2.username"></el-input>
      </el-form-item>

      <el-form-item label="密码"
                    prop="pass">
        <el-input type="password"
                  v-model="ruleForm2.pass"
                  auto-complete="off"></el-input>
      </el-form-item>

      <el-form-item label="确认密码"
                    prop="checkPass">
        <el-input type="password"
                  v-model="ruleForm2.checkPass"
                  auto-complete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
                   @click="submitForm('ruleForm2')">提交</el-button>
        <el-button @click="resetForm('ruleForm2')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Register } from "../../api/user";
import md5 from "md5";
export default {
  props: ["userRules"],
  data () {
    const { password, ...user } = this.userRules;

    var username = (rule, value, callback) => {
      if (!value) {
        callback(new Error("用户名不许为空"));
      } else {
        if (value.trim().length < 2) {
          callback(new Error(`用户名长度不小于${user.username.min}位`));
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value.trim() === "" || value.trim().length < password.min) {
        callback(new Error(`密码不能小于${password.min}位`));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm2: {
        pass: "",
        checkPass: "",
        username: "",
      },
      rules2: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        username: [{ validator: username, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let { pass: password, username } = this.ruleForm2;
          password = md5(password);

          Register({ password, username }).then((res) => {
            if (res.code) {
              this.$message({
                message: "注册成功",
                type: "success",
              });
              // 跳到登陆
              this.$emit("update:chunkName", "Login");
            } else {
              this.$message({
                message: "账号已存在",
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style></style>
